package cn.tedu.v1.service.impl;

import cn.tedu.v1.mapper.DepartmentOfficeMapper;
import cn.tedu.v1.mapper.DoctorsMapper;
import cn.tedu.v1.mapper.UserMapper;
import cn.tedu.v1.model.Appointment;
import cn.tedu.v1.mapper.AppointmentMapper;
import cn.tedu.v1.model.User;
import cn.tedu.v1.service.IAppointmentService;
import cn.tedu.v1.vo.AppointmentVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 预约列表 服务实现类
 * </p>
 *
 * @author tedu.cn
 * @since 2021-07-06
 */
@Service
@Slf4j
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper, Appointment> implements IAppointmentService {
    @Autowired
    private UserMapper userMapper;
    /*查询当前用户信息,应该写在userServiceImp里*/
    public User selectUserByUserName(String username ) {
        QueryWrapper<Appointment> query = new QueryWrapper<>();
        //获取当前登录用户的用户名或取当前用户对象
       User user=userMapper.findUserByUsername(username);
        System.out.println("查询了用户名");
        return user;
    }
    /*保存预约内容至数据库中*/
    @Resource
    private AppointmentMapper appointmentMapper;
    @Override
    public Appointment savaAppointment(AppointmentVo appointmentVo) {
        Appointment appointment = new Appointment();
        appointment.setIssuedTime(LocalDateTime.now())
                .setUserId(appointmentVo.getUserId())
                .setName(appointmentVo.getName())
                .setAge(appointmentVo.getAge())
                .setPhone(appointmentVo.getPhone())
                .setStatus(0)
                .setOfficeId(appointmentVo.getOfficeId())
                .setDescription(appointmentVo.getDescription())
                .setDoctorId(appointmentVo.getDoctorId())
                .setAppointTime(appointmentVo.getAppointtime())
                .setGender(appointmentVo.getSex());
        appointmentMapper.insert(appointment);
        log.debug("生成的预约:{}",appointment);
        return appointment;
    }
    @Resource
    DoctorsMapper doctorsMapper;
    @Resource
    DepartmentOfficeMapper departmentOfficeMapper;
    @Override
    public PageInfo<Appointment> getAppointments(Integer pageNum,Integer pageSize) {
        System.out.println("执行了getAppointments()方法");
        QueryWrapper<Appointment> query= new QueryWrapper<>();
        //查询出来的预约按照时间排序
        query.orderByDesc("appoint_time");
        PageHelper.startPage(pageNum,pageSize);
        log.debug("预约的pageSize的设置:"+pageSize);
        List<Appointment> appointmentList=appointmentMapper.selectList(query);
        for(Appointment a:appointmentList){
           a.setDoctorName(doctorsMapper.selectById(a.getDoctorId()).getName());
           a.setDepartmentName(departmentOfficeMapper.selectById(a.getOfficeId()).getOfficeName());
        }
       PageInfo<Appointment> pageInfo=new PageInfo<>(appointmentList);
        log.debug("所有预约的内容:"+pageInfo);
        return  pageInfo;

    }
    @Override
    public Integer updateAppointment(AppointmentVo appointmentVo) {
        Appointment appointment=new Appointment();
        appointment.setId(appointmentVo.getId())
                .setIssuedTime(LocalDateTime.now())
                .setUserId(appointmentVo.getUserId())
                .setStatus(0)
                .setOfficeId(appointmentVo.getOfficeId())
                .setDescription(appointmentVo.getDescription())
                .setDoctorId(appointmentVo.getDoctorId())
                .setAppointTime(appointment.getGender())
                .setName(appointmentVo.getName())
                .setAge(appointmentVo.getAge())
                .setPhone(appointmentVo.getPhone());
       appointmentMapper.updateById(appointment);
       return 1;

    }

    @Override
    public Integer deleteAppointment(Integer Id) {
        int num=appointmentMapper.deleteById(Id);
        return 1;
    }
}
